home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Snippets / Printing / Dashed-capped Lines / Dashed-capped Lines.p < prev    next >
Encoding:
Text File  |  1992-07-15  |  6.1 KB  |  244 lines  |  [TEXT/MPS ]

  1. {**
  2.  **     Program: Dashed-capped Lines
  3.  **
  4.  **     Version: 1.0    8/91
  5.  **
  6.  **        MPW 3.2 Pascal source.
  7.  **
  8.  **     Purpose:
  9.  **
  10.  **        Dashed-capped Lines demonstrates how to draw dashed line objects on PostScript
  11.  **        printers.  It also demonstrates how to use PostScript line caps with dashed
  12.  **        lines.
  13.  **        
  14.  **        This simple example does not deal with QuickDraw printers, on which it draws
  15.  **        solid black lines.
  16.  **        
  17.  **        (This is my Dashed Lines sample modified to use setlinecap to make round-ended lines.)
  18.  **        
  19.  **        - Dave Hersey 8/30/91
  20.  **
  21.  **        Macintosh Developer Technical Support
  22.  **
  23.  **
  24.  **}
  25.  
  26. PROGRAM Dashed_Lines;
  27.  
  28. USES
  29.     OSIntf, QuickDraw, ToolIntf, MacPrint;
  30.  
  31.  
  32. CONST
  33.     DashedLine            = 180;    (*    Begin PostScript line dashing            *)
  34.     DashedStop            = 181;    (*    End PostScript line dashing                *)
  35.     SetLineWidth        = 182;    (*    Set high resolution line width.         *)    
  36.     PostScriptHandle    = 192;    (*    Transfer a block of PostScript            *)    
  37.  
  38. TYPE
  39.     TDashedLineHdl = ^TDashedLinePtr;
  40.     TDashedLinePtr = ^TDashedLine;
  41.     TDashedLine = PACKED RECORD
  42.         offset:        SignedByte;
  43.         centered:    SignedByte;
  44.         dashed:        ARRAY [0..1] OF SignedByte;
  45.     END;
  46.  
  47.  
  48. {*------ SendPostScript ------------------------------------------------------------*}
  49.  
  50. PROCEDURE SendPostScript(theComment: Str255);
  51. VAR
  52.     PSCommand    : Str255;
  53.     CommandHdl    : Handle;
  54.     CRString    : Str255;
  55.     theError    : OSErr;
  56. BEGIN
  57.     CRString := ' ';
  58.     CRString[1] := CHR(13);
  59.     PSCommand := theComment;
  60.     PSCommand := CONCAT(PSCommand, CRString);
  61.     theError := PtrToHand(POINTER(ORD(@PSCommand) + 1), CommandHdl, LENGTH(PSCommand));
  62.     PicComment(PostScriptHandle, LENGTH(PSCommand), CommandHdl);
  63.     DisposHandle(CommandHdl);
  64. END;
  65.  
  66.  
  67.   {*------ DrawStuff -----------------------------------------------------------------*}
  68.  
  69. {**
  70.  **      DrawStuff draws QuickDraw objects with dashed lines in Postscript.
  71.  **}
  72.  
  73.  PROCEDURE DrawStuff (theWorld : Rect; theGPort : GrafPtr);
  74.  
  75. TYPE
  76.     widhdl = ^widptr;
  77.     widptr = ^widpt;
  78.     widpt = Point;
  79.  
  80.  VAR
  81.    oldPort      :     GrafPtr;
  82.    PSHdl        :    Handle;
  83.    arect        :    Rect;
  84.    Width        :    Widhdl;
  85.    dashedln     :    TDashedLineHdl;
  86.    myPic        :    PicHandle;
  87.  
  88.  
  89. BEGIN
  90.     GetPort (oldPort);
  91.  
  92.     SetPort (theGPort);
  93.     Dashedln := TDashedLineHdl(NewHandle(sizeof(tdashedline)));
  94.     Dashedln^^.offset := 0;       {No offset}
  95.     Dashedln^^.centered := 0;     {don’t center}
  96.     Dashedln^^.dashed[0] := 1;    {this is the length }
  97.     Dashedln^^.dashed[1] := 4;    {this means 4 points on, 4 points off }
  98.     
  99.     Width := widhdl(NewHandle(sizeof(widpt)));
  100.     Width^^.h := 1;                {denominator is 4}
  101.     Width^^.v := 2;               {numerator is 1}
  102.     
  103.     myPic := OpenPicture(theWorld);
  104.     PenSize(1,1);                {Set the pen size to 1 wide x 1 high }
  105.     ClipRect(theWorld);
  106.  
  107.     SendPostScript('1 setlinecap');
  108.     PicComment(DashedLine,GetHandleSize(Handle(dashedln)),Handle(dashedln)); 
  109.     PicComment(SetLineWidth,4,Handle(width));   {SetLineWidth to 8 pixels wide.}
  110.  
  111.     SetRect(arect,100,100,500,500);    {Draw some stuff in dash mode.}
  112.     FrameRect(aRect);
  113.     MoveTo(500,500);
  114.     Lineto(100,100);
  115.     MoveTo(100,500);
  116.     Lineto(500,100);
  117.  
  118.     InsetRect(arect,10,10);
  119.     FrameOval(aRect);
  120.     InsetRect(arect,10,10);
  121.     FrameOval(aRect);
  122.     InsetRect(arect,10,10);
  123.     FrameOval(aRect);
  124.     InsetRect(arect,10,10);
  125.     FrameOval(aRect);
  126.     InsetRect(arect,10,10);
  127.     FrameOval(aRect);
  128.     InsetRect(arect,10,10);
  129.     FrameOval(aRect);
  130.     InsetRect(arect,10,10);
  131.     FrameOval(aRect);
  132.     InsetRect(arect,10,10);
  133.     FrameOval(aRect);
  134.     InsetRect(arect,10,10);
  135.     FrameOval(aRect);
  136.  
  137.     PicComment(DashedStop,0,nil);    {DashedStop}
  138.  
  139.   ClosePicture;
  140.   DisposHandle(handle(width));           {Clean up}
  141.   DisposHandle(handle(dashedln));
  142.   DrawPicture(MyPic, theWorld);          {print it}
  143.   KillPicture(MyPic);    SetPort(oldPort);
  144.  
  145.  END;  {**  DrawStuff  **}
  146.  
  147.  
  148. {*------ PrintStuff ----------------------------------------------------------------*}
  149. {**
  150.  **        PrintStuff will call all of the nescessary Print Manager calls to print 
  151.  **        a document. It checks PrError() after each Print Manager call. If an error 
  152.  **     is found, all of the Print Manager open calls (i.e. PrOpen, PrOpenDoc...) 
  153.  **        will have a corresponding close call before the error is posted to the user. 
  154.  **        You want to use this approach to make sure the Print Manager closes properly 
  155.  **        and all temporary memory is released.
  156.  **}
  157.  
  158. PROCEDURE PrintStuff;
  159.  
  160. VAR
  161.   Loop,
  162.   NumberOfPages,
  163.   PageNumber        : Integer;
  164.   PrintError        : LongInt;
  165.   oldPort              : GrafPtr;
  166.   thePrRecHdl        : THPrint;
  167.   thePrPort            : TPPrPort;
  168.   theStatus            : TPrStatus;
  169.     
  170. BEGIN
  171.    GetPort(oldPort);
  172.     
  173.    thePrRecHdl := THPrint(NewHandle(SIZEOF(TPrint)));
  174.     
  175.    IF (MemError = noErr) AND (thePrRecHdl <> NIL) THEN
  176.     BEGIN
  177.        PrOpen;
  178.        IF (PrError = noErr) THEN
  179.         BEGIN
  180.            PrintDefault(thePrRecHdl);
  181.  
  182.            IF (PrError = noErr) THEN
  183.             BEGIN
  184.                IF (PrStlDialog(thePrRecHdl)) THEN
  185.                 BEGIN
  186.                    IF (PrJobDialog(thePrRecHdl)) THEN 
  187.                     BEGIN
  188.                           thePrPort := PrOpenDoc(thePrRecHdl, NIL, NIL);
  189.                                
  190.                       IF (PrError = noErr) THEN
  191.                         BEGIN
  192.  
  193.                              PrOpenPage(thePrPort, NIL);
  194.                                 
  195.                           IF (PrError = noErr) THEN
  196.                             BEGIN
  197.                               {**
  198.                                   rPage (IM II-150) is the printable area for the  
  199.                                   currently selected printer. By passing the current  
  200.                                   port to the draw routine, enables your app
  201.                                   to use the same routine to draw to the screen
  202.                                   and the printer's GrafPort.
  203.                                **}
  204.                                     
  205.                                DrawStuff (thePrRecHdl^^.prInfo.rPage, 
  206.                                            GrafPtr (thePrPort));
  207.                                  
  208.                              END;
  209.                             PrClosePage(thePrPort);
  210.                           END;
  211.                              
  212.                           PrCloseDoc(thePrPort);
  213.                              
  214.                           IF (thePrRecHdl^^.prJob.bJDocLoop = bSpoolLoop) and (PrError = noErr) THEN
  215.                                PrPicFile(thePrRecHdl, NIL, NIL, NIL, theStatus);
  216.  
  217.                       END;
  218.                   END;
  219.               END;
  220.           END;
  221.         
  222.         PrClose;
  223.  
  224.      END;
  225.  
  226. END;  {**  PrintStuff  **}
  227.  
  228.  
  229. {*------ main ----------------------------------------------------------------------*}
  230.  
  231. BEGIN
  232.     
  233.     InitGraf(@thePort);
  234.     InitFonts;
  235.     FlushEvents(everyEvent, 0);    
  236.     InitWindows;
  237.     InitMenus;
  238.     TEInit;
  239.     InitDialogs(NIL);
  240.     InitCursor;
  241.  
  242.     PrintStuff;
  243.  
  244. END. {**  main  **}